﻿@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using Microsoft.Extensions.DependencyInjection
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Core.Extension

@namespace ThingsGateway.Admin.Blazor
@inherits BasePopupComponentBase

<PModal Persistent Title=@AppService.I18n.T("授权资源") Value="Visible" Width=800 ValueChanged=@((a=>{if(!a)
        ClosePopupAsync();}))
         OnSave="OnRoleHasResourcesSaveAsync">

     @if (Visible)
    {
        <MSheet Outlined Class="ma-0 pa-2">
             <MRow Align="AlignTypes.Center">
                 <MCol>  <MLabel Class="ml-4 font-weight-black">@AppService.I18n.T("菜单") </MLabel>  </MCol>
                    <MDivider Vertical />
                 <MCol>  <MLabel Class="ml-4 font-weight-black">@AppService.I18n.T("按钮")</MLabel>    </MCol>
                </MRow>
            </MSheet>
        @foreach (var menu in _resTreeSelectors.Menu)
        {
            <MSheet Outlined Class="ma-0 pa-4">
                 <MRow Align="AlignTypes.Center">
                     <MCol>
                         <MListItem OnClick=@(()=>
                           {
                           if(_hasResources.Any(it=>it.MenuId==menu.Id))
                           _hasResources.RemoveWhere(it=>it.MenuId==menu.Id);
                           else if(!_hasResources.Any(it=>it.MenuId==menu.Id))
                           _hasResources.Add(new() {MenuId=menu.Id,ButtonInfo=menu.Button.Select(a=>a.Id).ToList()});
                           }) IsActive=@(_hasResources.Any(it=>it.MenuId==menu.Id))>
                             <ItemContent>
                                 <MListItemContent>
                                     <MListItemTitle>@menu.Title</MListItemTitle>
                                    </MListItemContent>
                                </ItemContent>
                            </MListItem>


                        </MCol>
                        <MDivider Vertical />
                     <MCol>
                         @GetButtonCore(menu)
                    </MCol>

                </MRow>

            </MSheet>
        }

    }

</PModal>




@code {
    RenderFragment GetButtonCore(ResTreeSelector.RoleGrantResourceMenu menu)
    {
        RenderFragment ViewSubMenu = null;

        foreach (var button in menu.Button ?? new())
        {
            ViewSubMenu +=
    @<MListItem Class="ml-6" OnClick=@( () =>
                {
                if(_hasResources.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.Contains(button.Id)==true)
                {
                _hasResources.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.RemoveWhere(it=>it==button.Id);
                }
                else
                {
                if( !(_hasResources.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.Any(it=>it==button.Id)==true))
                {
                if(!_hasResources.Any(it=>it.MenuId==menu.Id))
                {
                _hasResources.Add(new() {MenuId=menu.Id});
                }
                _hasResources.FirstOrDefault(it=>it.MenuId==menu.Id).ButtonInfo.Add(button.Id);
                }
                }
                }) IsActive=@(_hasResources.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.Contains(button.Id)==true)>
        <ItemContent>
            <MListItemContent>
                <MListItemTitle>@button.Title</MListItemTitle>
            </MListItemContent>
        </ItemContent>
    </MListItem>
        ;
        }

        return ViewSubMenu;
    }

}



